package com.duanyf.wiki.service;

import com.duanyf.wiki.domain.Doc;
import com.duanyf.wiki.domain.DocExample;
import com.duanyf.wiki.domain.DocExample.Criteria;
import com.duanyf.wiki.mapper.DocMapper;
import com.duanyf.wiki.req.DocQueryReq;
import com.duanyf.wiki.req.DocSaveReq;
import com.duanyf.wiki.resp.DocQueryResp;
import com.duanyf.wiki.resp.PageResp;
import com.duanyf.wiki.util.CopyUtil;
import com.duanyf.wiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @Description:
 * @Author: duanyf
 * @DateTime: 2022/9/16 0016 15:27
 */
@Service
public class DocService {

  //使用@Autowired会报错但是运行应该没有问题
  @Resource
  private DocMapper docMapper;

  @Resource
  private SnowFlake snowFlake;

  private static final Logger LOG=
          LoggerFactory.getLogger(DocService.class);

  public List<DocQueryResp> all(DocQueryReq req){
    DocExample docExample = new DocExample();

    //增加排序功能
    docExample.setOrderByClause("sort asc");

    //相当于where条件
    Criteria criteria = docExample.createCriteria();
    //动态sql
    if(!ObjectUtils.isEmpty(req.getName())){
      //不为空模糊查询
      criteria.andNameLike("%"+req.getName()+"%");
    }

    List<Doc> docList = docMapper.selectByExample(docExample);


    //持久层返回List<Doc>需要转成List<DocResp>再赋给pageResp
    List<DocQueryResp> respList = CopyUtil.copyList(docList, DocQueryResp.class);

    return respList;
  }

  public PageResp<DocQueryResp> list(DocQueryReq req){
    DocExample docExample = new DocExample();

    //增加排序功能
    docExample.setOrderByClause("sort asc");

    //相当于where条件
    Criteria criteria = docExample.createCriteria();
    //动态sql
    if(!ObjectUtils.isEmpty(req.getName())){
      //不为空模糊查询
      criteria.andNameLike("%"+req.getName()+"%");
    }

    //分页
    PageHelper.startPage(req.getPage(),req.getSize());
    List<Doc> docList = docMapper.selectByExample(docExample);

    //打印总页数和总行数
    PageInfo<Doc> pageInfo=new PageInfo<>(docList);
    LOG.info("总行数：{}",pageInfo.getTotal());
    LOG.info("总页数：{}",pageInfo.getPages());

    //持久层返回List<Doc>需要转成List<DocResp>再赋给pageResp
    List<DocQueryResp> respList = CopyUtil.copyList(docList, DocQueryResp.class);

    PageResp<DocQueryResp> pageResp=new PageResp<>();
    pageResp.setList(respList);
    pageResp.setTotal(pageInfo.getTotal());

    return pageResp;
  }

  /**
   * 保存
   */
  public void save(DocSaveReq req){
    Doc doc=CopyUtil.copy(req,Doc.class);
    //doc的id有值说明是更新，没有值是新增
    if (ObjectUtils.isEmpty(doc.getId())){
      //新增
      long nextId = snowFlake.nextId();//使用雪花算法得到id
      doc.setId(nextId);
      docMapper.insert(doc);
    }else {
      //更新
      docMapper.updateByPrimaryKey(doc);
    }
  }

  /**
   * 删除
   */
  public void delete(Long id){
    docMapper.deleteByPrimaryKey(id);
  }

  /**
   * 删除列表中的多个数据
   * @param ids
   */
  public void delete(List<String> ids){
    DocExample docExample = new DocExample();

    //相当于where条件
    Criteria criteria = docExample.createCriteria();
    List<Long> idL = ids.stream().map(s -> Long.parseLong(s.trim())).collect(
        Collectors.toList());
    criteria.andIdIn(idL);
    docMapper.deleteByExample(docExample);
  }
}
